from django.db import models
import uuid
# Create your models here.

class Task(models.Model):
    title = models.CharField(max_length=200)
    completed = models.BooleanField(default=False)
    created_at = models.DateTimeField(auto_now_add=True)
    
    def __str__(self):
        return self.title

class KnowledgeBase(models.Model):
    name = models.CharField(max_length=200)  # 知识库名
    created_at = models.DateTimeField(auto_now_add=True)  # 创建时间
    updated_at = models.DateTimeField(auto_now=True)  # 修改时间
    directory = models.CharField(max_length=255, unique=True, blank=True)  # 唯一文件目录

    def save(self, *args, **kwargs):
        # 在保存之前生成唯一目录
        if not self.directory:
            self.directory = f"knowledge_base_{self.id}"  # 生成目录名称
        super().save(*args, **kwargs)

    def __str__(self):
        return self.name

class KnowledgeBaseFile(models.Model):
    knowledge_base = models.ForeignKey(KnowledgeBase, on_delete=models.CASCADE)  # 关联知识库
    file = models.FileField(upload_to='knowledge_base_files/%Y/%m/%d/', default='')  # 文件字段

    # 新增字段
    resource_code = models.CharField(max_length=100, blank=True)  # 资料编号
    resource_name = models.CharField(max_length=200, blank=True)  # 资料名称
    resource_type = models.CharField(max_length=100, blank=True)  # 资料分类
    resource_source = models.CharField(max_length=200, blank=True)  # 资料来源
    resource_quality = models.CharField(max_length=100, blank=True)  # 资料质量
    version_number = models.CharField(max_length=100, blank=True)  # 版本号
    resource_description = models.TextField(blank=True)  # 资料描述
    resource_contact_person = models.CharField(max_length=100, blank=True)  # 资料联系人
    contact_phone = models.CharField(max_length=20, blank=True)  # 联系电话
    contact_email = models.EmailField(blank=True)  # 联系Email
    notes = models.TextField(blank=True)  # 备注
    def __str__(self):
        return self.file.name
    def save(self, *args, **kwargs):
        # 动态生成文件路径
        if self.knowledge_base and self.knowledge_base.directory:
            self.file.field.upload_to = f'knowledge_base_files/{self.knowledge_base.directory}/'
        super().save(*args, **kwargs)

class ProcessingData(models.Model):
    file = models.FileField(upload_to='knowledge_base_files/%Y/%m/%d/',default='')  # 文件字段
    knowledge_base = models.ForeignKey(KnowledgeBase, related_name='processing_data_files', on_delete=models.CASCADE)  # 关联知识库

    # knowledge_base_id = models.IntegerField(blank=True, null=True) 
    result_code = models.CharField(max_length=100)  # 结果编号
    result_name = models.CharField(max_length=200)  # 结果名称
    result_type = models.CharField(max_length=100)  # 结果分类
    method_used = models.CharField(max_length=200)  # 所用方法
    method_parameters = models.TextField(blank=True)  # 所用方法参数
    instructions = models.TextField(blank=True)  # 指示
    notes = models.TextField(blank=True)  # 备注
    file_name = models.CharField(max_length=200)  # 文件名
    original_file_id = models.CharField(max_length=100)  # 原始文件id
    resource_directory = models.CharField(max_length=255)  # 所属资料库

    def __str__(self):
        return self.result_name  # 返回结果名称作为字符串表示
    def save(self, *args, **kwargs):
        # 动态生成文件路径
        if self.knowledge_base and self.knowledge_base.directory:
            self.file.field.upload_to = f'knowledge_processing_files/{self.knowledge_base.directory}/'
        super().save(*args, **kwargs)